package com.wrupple.muba.widget.client.widgets.panels;

import java.util.ArrayList;
import java.util.List;

import com.google.gwt.dom.client.Style.Unit;
import com.google.gwt.i18n.client.LocaleInfo;
import com.google.gwt.user.client.ui.DockLayoutPanel;
import com.google.gwt.user.client.ui.DockLayoutPanel.Direction;
import com.google.gwt.user.client.ui.LayoutPanel;
import com.google.gwt.user.client.ui.Widget;
import com.wrupple.muba.util.client.layout.LayoutPositioner;

public class PanelWithToolbarLayoutDelegate implements LayoutPositioner {
	
	static private class ToolbarLayoutAction {
		Widget toolbar;
		DockLayoutPanel.Direction direction;
		int size;
		public ToolbarLayoutAction(Widget toolbar, Direction direction, int size) {
			super();
			this.toolbar = toolbar;
			this.direction = direction;
			this.size = size;
		}
		public Widget getToolbar() {
			return toolbar;
		}
		public DockLayoutPanel.Direction getDirection() {
			return direction;
		}
		public int getSize() {
			return size;
		}
		
	}

	private LayoutPanel main;
	private TransitionPanel container;
	private List<ToolbarLayoutAction> layoutActions;

	@Override
	public void initialize(LayoutPanel main,TransitionPanel container) {
		this.main=main;
		main.clear();
		this.container=container;
		main.add(container);
		layoutActions=new ArrayList<PanelWithToolbarLayoutDelegate.ToolbarLayoutAction>();
	}

	@Override
	public void animate(int duration) {
		Widget toolbar;
		DockLayoutPanel.Direction direction;
		double size;
		Unit unit=Unit.PX;
		double top=0;
		double right=0;
		double left =0;
		double bottom=0;
		
		for(ToolbarLayoutAction action:this.layoutActions){
			toolbar=action.getToolbar();
			direction=action.getDirection();
			size=action.getSize();
			main.add(toolbar);
			switch (direction) {
			case NORTH:
				main.setWidgetTopHeight(toolbar, top-size, unit, size, unit);
				main.setWidgetLeftRight(toolbar, left, unit, right, unit);
				break;
			case EAST:
				main.setWidgetRightWidth(toolbar, right-size, unit, size, unit);
				main.setWidgetTopBottom(toolbar, top, unit, bottom, unit);
				break;
			case WEST:
				main.setWidgetLeftWidth(toolbar, left-size, unit, size, unit);
				main.setWidgetTopBottom(toolbar, top, unit, bottom, unit);
				break;
			case SOUTH:
				main.setWidgetBottomHeight(toolbar, bottom-size, unit, size, unit);
				main.setWidgetLeftRight(toolbar, left, unit, right, unit);
				break;
			case LINE_START:
				if(LocaleInfo.getCurrentLocale().isRTL()){
					main.setWidgetRightWidth(toolbar, right-size, unit, size, unit);
				}else{
					main.setWidgetLeftWidth(toolbar, left-size, unit, size, unit);
				}
				main.setWidgetTopBottom(toolbar, top, unit, bottom, unit);
				break;
			case LINE_END:
				if(LocaleInfo.getCurrentLocale().isRTL()){
					main.setWidgetLeftWidth(toolbar, left-size, unit, size, unit);
				}else{
					main.setWidgetRightWidth(toolbar, right-size, unit, size, unit);
				}
				main.setWidgetTopBottom(toolbar, top, unit, bottom, unit);
				break;
			}
		}
		main.forceLayout();
		
		for(ToolbarLayoutAction action:this.layoutActions){
			toolbar=action.getToolbar();
			direction=action.getDirection();
			size=action.getSize();
			switch (direction) {
			case NORTH:
				main.setWidgetTopHeight(toolbar, top, unit, size, unit);
				main.setWidgetLeftRight(toolbar, left, unit, right, unit);
				top+=size;
				break;
			case EAST:
				main.setWidgetRightWidth(toolbar, right, unit, size, unit);
				main.setWidgetTopBottom(toolbar, top, unit, bottom, unit);
				right+=size;
				break;
			case WEST:
				main.setWidgetLeftWidth(toolbar, left, unit, size, unit);
				main.setWidgetTopBottom(toolbar, top, unit, bottom, unit);
				left+=size;
				break;
			case SOUTH:
				main.setWidgetBottomHeight(toolbar, bottom, unit, size, unit);
				main.setWidgetLeftRight(toolbar, left, unit, right, unit);
				bottom+=size;
				break;
			case LINE_START:
				if(LocaleInfo.getCurrentLocale().isRTL()){
					main.setWidgetRightWidth(toolbar, right, unit, size, unit);
					right+=size;
				}else{
					main.setWidgetLeftWidth(toolbar, left, unit, size, unit);
					left+=size;
				}
				main.setWidgetTopBottom(toolbar, top, unit, bottom, unit);
				break;
			case LINE_END:
				if(LocaleInfo.getCurrentLocale().isRTL()){
					main.setWidgetLeftWidth(toolbar, left, unit, size, unit);
					left+=size;
				}else{
					main.setWidgetRightWidth(toolbar, right, unit, size, unit);
					right+=size;
				}
				main.setWidgetTopBottom(toolbar, top, unit, bottom, unit);
				break;
			}
		}
		main.setWidgetTopBottom(container, top, unit, bottom, unit);
		main.setWidgetLeftRight(container, left, unit, right, unit);
		
		main.animate(duration);
	}

	
	@Override
	public void addAtPosition(Widget toolbar, Direction direction, int size) {
		ToolbarLayoutAction action = new ToolbarLayoutAction(toolbar, direction, size);
		this.layoutActions.add(action);
	}


}
